package com.tanhua.dubbo.api;

import cn.hutool.core.thread.ThreadUtil;
import com.tanhua.model.domain.mongodb.Friend;
import com.tanhua.model.domain.mongodb.Movement;
import com.tanhua.model.domain.mongodb.MovementTimeLine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author maorf
 * @version 1.0
 */
@Service
public class TimeLineService {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Async
    public void saveTimeLine(Movement movement) {
        Criteria criteria = Criteria.where("userId").is(movement.getUserId());
        Query query = Query.query(criteria);
        List<Friend> friends = mongoTemplate.find(query, Friend.class);
        ThreadUtil.sleep(20000);
        for (Friend friend : friends) {
            MovementTimeLine movementTimeLine = new MovementTimeLine();
            movementTimeLine.setMovementId(movement.getId());
            movementTimeLine.setCreated(System.currentTimeMillis());
            movementTimeLine.setFriendId(friend.getFriendId());
            movementTimeLine.setUserId(movement.getUserId());
            mongoTemplate.save(movementTimeLine);
        }
    }
}
